package com.wisdomparking.tools;

import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.wisdomparking.bean.Parking;
import com.wisdomparking.bean.Rawdata;
import com.wisdomparking.service.ParkingService;
import com.wisdomparking.service.RawdataService;
import com.wisdomparking.service.siwo.resbean.Res;

/**
 * 实时数据处理
 */
public class ToolDataHandle {
	public static void dataHandle(RawdataService rawdataService, ParkingService parkingService, Rawdata rawData) {
		if (ToolValid.isNotNull(rawData) && ToolValid.isNotEmpty(rawData.getIdentification())) {
			Parking parking = parkingService.getParkingByIdentification(rawData.getIdentification());

			if (ToolValid.isNotNull(parking) && ToolValid.isNotEmpty(parking.getDataHandle())) {

				switch (parking.getDataHandle()) {
				case "siwo":
					SIWO_DataHandle(parkingService, parking, rawData.getRealTimeData());
					break;

				default:
					break;
				}
			}
		}
	}

	/**
	 * 思沃数据处理
	 * 
	 * @param parkingService
	 * @param parking
	 * @param realTimeData
	 */
	@SuppressWarnings("unchecked")
	private static void SIWO_DataHandle(ParkingService parkingService, Parking parking, String realTimeData) {
		Res res = new Gson().fromJson(realTimeData, Res.class);
		
		List<Map<String, Float>> lists = (List<Map<String, Float>>) res.getData();
		String all_space = String.valueOf(lists.get(0).get("all_space")).substring(0,String.valueOf(lists.get(0).get("all_space")).indexOf("."));
		String free_space = String.valueOf(lists.get(0).get("free_space")).substring(0, String.valueOf(lists.get(0).get("free_space")).indexOf("."));
		
		parking.setParkingTotalB(Integer.parseInt(all_space));
		parking.setLightdutyVehicleB(Integer.parseInt(all_space));
		parking.setSurplusLightdutyVehicleB(Integer.parseInt(free_space));
		parkingService.updateParking(parking, false);
	}
}
